﻿#include "HomeWork0917.h"

USING_NS_CC;

Scene * HomeWork0917::createScene()
{
	return HomeWork0917::create();
}

bool HomeWork0917::init()
{
	if (!Scene::init())
	{
		return false;
	}
	
	//加载素材
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("GameSence.plist", "GameSence.png");

	CommonFunction::getChineseWithkey("b");

	//log("%s", CommonFunction::getChinese("b").c_str());


	//创建4个玩家
	

	visibleSize = Director::getInstance()->getVisibleSize();
	//游戏背景
	Sprite *background = Sprite::create("bg2.jpg");
	background->setPosition(Vec2(visibleSize.width/2, visibleSize.height / 2));
	addChild(background);

	//Sprite *dawang = Sprite::createWithSpriteFrameName("dawang.png");
	//dawang->setPosition(Vec2(VISIBLE_SIZE.width / 2, VISIBLE_SIZE.height / 2));
	//addChild(dawang);

	//Sprite *xiaowang = Sprite::createWithSpriteFrameName("xiaowang.png");
	//xiaowang->setPosition(Vec2(VISIBLE_SIZE.width / 2, VISIBLE_SIZE.height / 2));
	//addChild(xiaowang);

	//for (int i = 1; i < 14; i++)
	//{
	//	Sprite *cardSprite = Sprite::createWithSpriteFrameName(StringUtils::format("hei-%d.png", i));
	//	cardSprite->setPosition(Vec2(cardSprite->getBoundingBox().size.width / 2 + i * 60, VISIBLE_SIZE.height / 2));
	//	addChild(cardSprite);
	//}


	//关闭游戏场景按钮
	MenuItemImage *closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png");
	closeItem->setCallback(CC_CALLBACK_1(HomeWork0917::closeItemClicked,this));
	closeItem->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	closeItem->setPosition(Vec2(-visibleSize.width/2, visibleSize.height/2));
	closeItem->setScale(3);

	///开始游戏按钮
	MenuItemFont  *beginItem = MenuItemFont::create(CommonFunction::getChineseWithkey("beginGame")->getCString(), CC_CALLBACK_1(HomeWork0917::beginGame, this));

	Menu *menu = Menu::create(closeItem,beginItem, NULL);
	addChild(menu);

	self_pos = 1;
	for (int i = 1; i <= 4; i++)
	{
		Player *player = new Player();
		player->account = StringUtils::format("player%d", i);
		player->position = i;
		loadPlayerUI(player);
	}

	return true;
}

void HomeWork0917::setRoomId(int roomId)
{
	Label *label = Label::createWithSystemFont(StringUtils::format("room id = %d", roomId), "Arial", 50);
	label->setAnchorPoint(Vec2::ANCHOR_TOP_RIGHT);
	label->setPosition(Vec2(VISIBLE_SIZE.width - 30, VISIBLE_SIZE.height - 30));
	label->enableOutline(Color4B(90, 221, 247, 255), 3);
	addChild(label);
}

void HomeWork0917::loadPlayerUI(Player * player)
{
	player->pos_char = getPlayerCharPosition(player->position);

	//创建玩家节点包括了该玩家的ui信息等
	player->createPlayerNode();
	addChild(player->node);

	switch (player->pos_char)
	{
	case 's':
		player_self = player;
		break;
	case 'r':
		player_right = player;
		break;
	case 'f':
		player_front = player;
		break;
	case 'l':
		player_lelf = player;
		break;
	}
}

void HomeWork0917::beginGame(Ref * sender)
{
	CommonFunction::getChineseWithkey("beginGame");

	//洗牌
	int cards[54];
	CardGame *game = new CardGame();
	game->shuffle(cards, 54);

	//发牌
	player_self->initCards();
	player_right->initCards();
	player_lelf->initCards();
	player_front->initCards();
	Player *currentPlayer;//记录当前操作的玩家
	int start_pos = 3;//先发给3号

	for (int i = 0; i < 54; i++)
	{
		char pos_char = getPlayerCharPosition((start_pos+i-1)%4+1);

		//💗3
		if (cards[i] == 41)
		{
			game->first = pos_char;
		}
		//♦10
		if (cards[i] == 22)
		{
			game->red10_fk = pos_char;
		}
		//💗10
		if (cards[i] == 48)
		{
			game->red10_hx = pos_char;
		}
		switch (pos_char)
		{
		case 's':
			currentPlayer=player_self;
			break;
		case 'r':
			currentPlayer = player_right;
			break;
		case 'f':
			currentPlayer = player_front;
			break;
		case 'l':
			currentPlayer = player_lelf;
			break;
		}
		currentPlayer->cards[currentPlayer->countCards()] = cards[i];
	}

	player_self->sortCards();

	//显示自己的牌
	for (int i = 0; i < player_self->countCards(); i++)
	{
		string file_name = "";
		//大王小王
		if (player_self->cards[i] == 52)
		{
			//小王
			file_name = "xiaowang.png";
		}
		if (player_self->cards[i] == 53)
		{
			//大王
			file_name = "dawang.png";
		}
		else {
			//如何得到花色
			int flower = player_self->cards[i] / 13 + 1;
			int value = player_self->cards[i] % 13 + 1;
			switch (flower)
			{
			case 1://♠
				file_name = StringUtils::format("hei-%d.png", value);
				break;
			case 2://♦
				file_name = StringUtils::format("fk-%d.png", value);
				break;
			case 3://♣
				file_name = StringUtils::format("mh-%d.png", value);
				break;
			case 4://💗
				file_name = StringUtils::format("hong-%d.png", value);
				break;
			}
		}
		
		Sprite *cardSprite = Sprite::createWithSpriteFrameName(file_name);
		cardSprite->setPosition(Vec2(cardSprite->getBoundingBox().size.width / 2 + i * 60, visibleSize.height / 2));
		addChild(cardSprite);
	}
}


void HomeWork0917::closeItemClicked(Ref * sender)
{
	Director::getInstance()->replaceScene(HomeWork0910::createScene());
}

//s 表示 self自己
//l 表示 left上家
//r 表示 right下家
//f 表示 front对家
char HomeWork0917::getPlayerCharPosition(int pos)
{
	if (self_pos == 1)//当自己坐在1上
	{
		if (pos == 2)return 'r';//则2在右边
		if (pos == 3)return 'f';//3在对面
		if (pos == 4)return 'l';//4在左边
	}
	if (self_pos == 2)//当自己坐在2上
	{
		if (pos == 1)return 'l';//1在左边
		if (pos == 3)return 'r';//3在右边
		if (pos == 4)return 'f';//4在对面
	}
	if (self_pos == 3)//当自己坐在3上
	{
		if (pos == 1)return 'f';
		if (pos == 2)return 'l';
		if (pos == 4)return 'r';
	}
	if (self_pos == 4)//当自己坐在4上
	{
		if (pos == 1)return 'r';
		if (pos == 2)return 'f';
		if (pos == 3)return 'l';
	}
	return 's';
}
